package com.bert.controller;

import com.bert.entity.LogEntity;
import com.bert.entity.TextEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/log")
public class LogController {

    @GetMapping("/number")
    public LogEntity test(int number){
        long start = System.currentTimeMillis();
        for (int i = 0; i < number; i++) {
            log.info(TextEntity.text2 + "info:{}", i);
        }
        long end = System.currentTimeMillis();
        long sum = end - start;
        String averageNumber = String.format("%.2f", (double) number/sum*1000);
        return new LogEntity(200, "success", sum+"ms", sum/number+"ms", averageNumber+" n/s");
    }

    /**
     * 日志测试
     * @param number 循环次数
     * @param logsNumber 每次循环打印日志次数
     * @return 打印日志消耗平均时间毫秒ms
     */
    @GetMapping("/for")
    LogEntity testLog(int number, int logsNumber){
        long sum = 0;
        for (int i = 0; i < number; i++) {
            long start = System.currentTimeMillis();
            for (int j = 0; j < logsNumber; j++) {
                log.info("log4j2Test:{},{}", i, j);
            }
            long end = System.currentTimeMillis();
            sum += (end - start);
        }

        String averageNumber = String.format("%.2f", (double) logsNumber/sum*1000);
        return new LogEntity(200, "success", sum+"ms", sum/number+"ms", averageNumber+" n/s");
    }

    @GetMapping("/OOM")
    public String OOM(){
        //虚拟机调参: -Xms24m -Xmx24m -XX:+PrintGCDetails
        //虚拟机设置: -Xms1024m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError
        String str = "studyJvmDayByDay";
        while (true){
            str += str + new Random().nextInt(88888888) + new Random().nextInt(99999999);
        }
//        return str;
    }


}
